﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CS_WLLXSX_Query : BasePage
    {
        protected string wllxbh;
        CS_WLLX_Dao daoWLLX = new CS_WLLX_Dao();
        CS_WLLXSX_Dao daoWLLXSX = new CS_WLLXSX_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            wllxbh = Request.QueryString["wllxbh"];

            if (!IsPostBack)
            {
                string vpath = Request.QueryString[WLTree.VPATH];
                if (vpath != null)
                {
                    Cache.Insert(WLTree.VPATH, vpath);
                }

                this.lbTitle.Text = "物料类型属性列表";
                BindGridview();
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();
            List<CS_WLLXSX> list;
            if ((wllxbh != null))
            {
                list = daoWLLXSX.Query(wllxbh);
            }
            else
            {
               list = daoWLLXSX.QueryAll();
            }

            List<CS_WLLXSX_Ex> listEx = new List<CS_WLLXSX_Ex>();
            CS_WLLXSX_Ex ce;
            foreach (CS_WLLXSX c in list)
            {
                ce = new CS_WLLXSX_Ex();
                ce.BH = c.BH;
                ce.MC = c.MC;
                ce.WLLXBH = c.WLLXBH;
                ce.WLLXMC = daoWLLX.Query(c.WLLXBH).MC;
                listEx.Add(ce);
            }


            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string bh = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string lxbh = GridView1.Rows[e.RowIndex].Cells[2].Text;
            int result = daoWLLXSX.Delete(lxbh, bh);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
                Cache.Insert(WLTree.LEVEL, WLTree.WLSX);

                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "onSuccess", "<script type='text/javascript'>parent.refresh('tree');</script>", false);
            }
        }

        protected class CS_WLLXSX_Ex
        {
            public string BH { get; set; }
            public string WLLXBH { get; set; }
            public string MC { get; set; }
            public string WLLXMC { get; set; }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (wllxbh != null)
            {
                Response.Redirect(string.Format("CS_WLLXSX_Add.aspx?wllxbh={0}", wllxbh));
            }
            else
            {
                Response.Redirect("CS_WLLXSX_Add.aspx");
            }
        }
    }
}